var MoveMenu = {};
MoveMenu.editMode = false;
MoveMenu.list = null;
MoveMenu.init = function(list) {
    MoveMenu.list = list;
    
    // 菜单点击的处理
    var f = function(targetGame) {
        if (window.notBetUI) {
            window.notBetUI = false;
            urlInfo.params.game = targetGame;
            switch_page("common", $(".content-wrap"));
        }
        else
            switchGame(targetGame);
    } 
    $(".lotterys a").click(function(e) {
        if (MoveMenu.editMode) return;
        var g = $(e.currentTarget).attr("game");
        if (!g) return;
        f(g);
    });

    $("#homeMenus a .editbtn").click(function(e) {
        var a = $(e.currentTarget).parent();
        var g = a.attr("game");
        var index = -1;
        for (var i in MoveMenu.list) {
            if (MoveMenu.list[i].game == g) {
                index = parseInt(i);
                break;
            }
        }
        if (index != -1) {
            MoveMenu.list.splice(index, 1);
        }
        MoveMenu.list.push({
            game: g,
        });
        MoveMenu.sync();
        MoveMenu.setEditMode();
    })
    $("#gamebox a .editbtn").click(function(e) {
        // 列表超过9个就不行再添加了
        var a = $(e.currentTarget).parent();
        var g = a.attr("game");

        if ($("#homeMenus a").length >= 9) {
            alert2("最多可以添加9个游戏！", true);
            return;
        }
        var index = -1;
        for (var i in MoveMenu.list) {
            if (MoveMenu.list[i].game == g) {
                index = parseInt(i);
                break;
            }
        }
        if (index != -1) {
            MoveMenu.list.splice(index, 1);
        }
        MoveMenu.list.push({
            game: g,
            top: true,
        });
        MoveMenu.sync();
        MoveMenu.setEditMode();
    })

    MoveMenu.register();
}

MoveMenu.sync = function() {
    var list = [];
    for (var i in MoveMenu.list) {
        if (MoveMenu.list[i].top)
            list.push(MoveMenu.list[i].game);
    }
    GameList.local = list;
    GameList.draw();
}

MoveMenu.refresh = function() {
    var list = MoveMenu.list;
    var arr = $("#homeMenus a");
    var tops = [];
    for (var i = 0; i < arr.length; i++) {
        tops.push($(arr[i]));
    }
    tops.sort(function(a, b) {
        return a.position().left - b.position().left;
    });
    
    list = MoveMenu.list = [];
    for (var i in tops) {
        list.push({
            game: tops[i].attr("game"),
            top: true,
        });
    }
    var arr = $("#moreGameList a");
    for (var i = 0; i < arr.length; i++) {
        if (!$(arr[i]).attr("game")) continue;
        list.push({
            game: $(arr[i]).attr("game")
        });
    }
    return list;
}

MoveMenu.register = function() {
    if (MoveMenu._init) return;
    MoveMenu._init = true;

    // 更多游戏的处理
    $("#moreGameBtn").on({
        mouseover: function() {
            if (!MoveMenu.editMode) {
                $(".actions .action-edit").html("编辑");
                $("#moreGameList").show();
            }
        },
        mouseout: function() {
            if (!MoveMenu.editMode)
                $("#moreGameList").hide();
        },
        click: function() {
            if (!MoveMenu.editMode)
                $("#moreGameList").hide();
        },
    });

    $(".actions .action-edit").click(function() {
        if (MoveMenu.editMode) {
            // 保存列表
            MoveMenu.editMode = false;
            MoveMenu.saveLocalMenus(MoveMenu.refresh());
            MoveMenu.refresh();
            return;
        }
        MoveMenu.setEditMode();
    })
    $(".actions .action-cancel").click(function() {
        MoveMenu.editMode = false;
        GameList.draw();
    });
}

MoveMenu.setEditMode = function() {
    MoveMenu.editMode = true;
    $("i.editbtn").show();
    $(".actions .action-cancel").show();
    $(".actions .action-edit").html("完成");
    $("#homeMenus a").Tdrag({
        axis: "x",
        pos: true,
        cbEnd: function() {
            MoveMenu.refresh();
        },
        dragChange: true,
        changeMode: "sort",
        disable: false,
    });
}

MoveMenu.saveLocalMenus = function() {
    var arr = [];
    for (var i in MoveMenu.list) {
        var d = MoveMenu.list[i];
        if (d.top) arr.push(d.game);
    }
    Util.setMenuGames(arr);
}
